/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  PictureDraw.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-15 上午11:22:38
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-15          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.picture;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

 /**
 * <pre> 
 * 图片绘制类，绘制验证码。
 * </pre>
 * @ClassName  PictureDraw
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class PictureDraw {

	/**
	 * width 宽
	 */
	private int width = 200;

	/**
	 * height 高
	 */
	private int height = 100;

	/**
	 * line 干扰线的条数
	 */
	private int line = 5;  

	/**
	 * content 画入图片的内容
	 */
	private String content = "";

	/**
	 * backColor 背景色
	 */
	private Color backColor = new Color(255, 255, 255);
	
	/**
	 * fontColor 字体色
	 */
	private Color fontColor = new Color(0, 0, 0);

	private Random random = new Random();
	
	/**
	 * <pre>
	 * 构造函数。
	 * </pre>
	 *
	 * @param width 绘制图片宽
	 * @param height 绘制图片高
	 * @param line 干扰线
	 * @param content 内容 
	 */
	public PictureDraw(int width, int height, int line, String content) {
		this.width = width;
		this.height = height;
		this.line = line;
		this.content = content;
	}
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Color getBackColor() {
		return backColor;
	}

	public void setBackColor(Color backColor) {
		this.backColor = backColor;
	}

	public Color getFontColor() {
		return fontColor;
	}

	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
	}

	/**
	 * <pre>
	 * 生成一个随机图片。
	 * </pre>
	 *
	 * @return   
	 */
	public BufferedImage drawDynamicImage() {
		// 创建BufferedImage
		BufferedImage image = new BufferedImage(this.getWidth(),
				this.getHeight(), BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics graphics = image.getGraphics();
		// 画背景色
		graphics.setColor(this.getBackColor());
		// 填充整个画布
		graphics.fillRect(0, 0, this.getWidth(), this.getHeight());

		// 设置字体
		graphics.setFont(new Font(null, Font.BOLD, random.nextInt(20) + 48));
		// 设置字体颜色
		graphics.setColor(this.fontColor);
		// 画内容
		graphics.drawString(this.getContent(),
				this.getWidth() / (this.content.length()),
				this.getHeight() - 30);
		// 画干扰线
		for (int j = 0; j < this.getLine(); j++) {
			graphics.setColor(getRandomColor());
			graphics.drawLine(random.nextInt(this.getWidth()),
					random.nextInt(this.getHeight()),
					random.nextInt(this.getWidth()),
					random.nextInt(this.getHeight()));
		}
		return image;
	}

	/**
	 * <pre>
	 * 随机颜色。
	 * </pre>
	 *
	 * @return   
	 */
	private Color getRandomColor() {
		return new Color(random.nextInt(255), random.nextInt(255),
				random.nextInt(255));
	}
}
